﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using Kingdee.BOS;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Core.Metadata.ControlElement;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Core.Report.PlugIn;
using Kingdee.BOS.Core.Report.PlugIn.Args;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Contracts.Report;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.CommonFilter;
using Kingdee.BOS.Core.CommonFilter.PlugIn;
using Kingdee.BOS.Core.CommonFilter.PlugIn.Args;



namespace KMXMBillService
{
    [Description("报表金额字段显示千分位")]
    [Kingdee.BOS.Util.HotUpdate]
    public class KMXMEdit : AbstractSysReportPlugIn
    {

        /// <para
        public override void FormatCellValue(Kingdee.BOS.Core.Report.PlugIn.Args.FormatCellValueArgs args)
        {

            base.FormatCellValue(args);


            if (!args.Header.FieldName.EqualsIgnoreCase("FAcctNumber") && !args.Header.FieldName.EqualsIgnoreCase("FAcctName")  && !args.Header.FieldName.EqualsIgnoreCase("FAXMNumber")
                && !args.Header.FieldName.EqualsIgnoreCase("FAXMName") && !args.Header.FieldName.EqualsIgnoreCase("FBXMNumber") && !args.Header.FieldName.EqualsIgnoreCase("FBXMName") && !args.Header.FieldName.EqualsIgnoreCase("FADepNumber")
                && !args.Header.FieldName.EqualsIgnoreCase("FADepName") && !args.Header.FieldName.EqualsIgnoreCase("FBDepNumber") && !args.Header.FieldName.EqualsIgnoreCase("FBDepName") && !args.Header.FieldName.EqualsIgnoreCase("FCDepNumber")
                && !args.Header.FieldName.EqualsIgnoreCase("FCDepName") && !args.Header.FieldName.EqualsIgnoreCase("FDDepNumber") && !args.Header.FieldName.EqualsIgnoreCase("FDDepName") && !args.Header.FieldName.EqualsIgnoreCase("FEDepNumber")
                && !args.Header.FieldName.EqualsIgnoreCase("FEDepName") && !args.Header.FieldName.EqualsIgnoreCase("FFDepNumber") && !args.Header.FieldName.EqualsIgnoreCase("FFDepName") && !args.Header.FieldName.EqualsIgnoreCase("FID")
                && !args.Header.FieldName.EqualsIgnoreCase("FAcctNumber") && !args.Header.FieldName.EqualsIgnoreCase("FAcctName"))
                     {
                         args.FormateValue = String.Format("{0:N}", args.Value);
                     }
        
        }

        public override void CellDbClick(CellEventArgs Args)
        {
            DynamicObject dyFilter = this.SysReportModel.FilterParameter.CustomFilter;
            DataRow CURDR = this.SysReportModel.DataSource.Rows[Args.CellRowIndex - 1];

            string FACCTBOOKNAME, FPERIOD, FCURRENCYID, FPOSTED, FormId;
            string FACCOUNTBOOKID, FACCTORGID, FFORBIDSTATUS, FDepC,FDepNO,FXMNO;

            FCURRENCYID = "";
            FACCOUNTBOOKID = "";
            FACCTORGID = "";
            FDepNO="";
            FXMNO="";
            FACCTBOOKNAME = "";

            string FAcctNumber = Convert.ToString(CURDR.ItemArray[1]);
            string FAcctName = Convert.ToString(CURDR.ItemArray[2]);
            string FXMNumber = Convert.ToString(CURDR.ItemArray[4]);
            string FDepNumber = Convert.ToString(CURDR.ItemArray[10]);
            string FAXMNumber = Convert.ToString(CURDR.ItemArray[6]);
            string FBXMNumber = Convert.ToString(CURDR.ItemArray[8]);
            string FADepNumber = Convert.ToString(CURDR.ItemArray[12]);
            string FBDepNumber = Convert.ToString(CURDR.ItemArray[14]);
            string FCDepNumber = Convert.ToString(CURDR.ItemArray[16]);
            string FDDepNumber = Convert.ToString(CURDR.ItemArray[18]);
            string FEDepNumber = Convert.ToString(CURDR.ItemArray[20]);
            string FFDepNumber = Convert.ToString(CURDR.ItemArray[22]);

            if (FAXMNumber=="")
            {
                FXMNO = "";
            }
            if (FADepNumber == "")
            {
                FDepNO = "";
            }
            //一级部门
            if (FADepNumber != "" && FBDepNumber == "" && FCDepNumber == "" && FDDepNumber == "" && FEDepNumber == "" && FFDepNumber == "")
            {
                FDepNO = FADepNumber;
            }
            //二级部门
            if (FADepNumber != "" && FBDepNumber != "" && FCDepNumber == "" && FDDepNumber == "" && FEDepNumber == "" && FFDepNumber == "")
            {
                FDepNO = FBXMNumber;
            }
            //三级部门
            if (FADepNumber != "" && FBDepNumber != "" && FCDepNumber != "" && FDDepNumber == "" && FEDepNumber == "" && FFDepNumber == "")
            {
                FDepNO = FCDepNumber;
            }
            //四级部门
            if (FADepNumber != "" && FBDepNumber != "" && FCDepNumber != "" && FDDepNumber != "" && FEDepNumber == "" && FFDepNumber == "")
            {
                FDepNO = FDDepNumber;
            }
            //五级部门
            if (FADepNumber != "" && FBDepNumber != "" && FCDepNumber != "" && FDDepNumber != "" && FEDepNumber != "" && FFDepNumber == "")
            {
                FDepNO = FEDepNumber;
            }
            //六级部门
            if (FADepNumber != "" && FBDepNumber != "" && FCDepNumber != "" && FDDepNumber != "" && FEDepNumber != "" && FFDepNumber != "")
            {
                FDepNO = FFDepNumber;
            }
            //一级项目
            if (FAXMNumber != "" && FBXMNumber == "")
            {
                FXMNO = FAXMNumber;
            }
            //二级部门
            if (FAXMNumber != "" && FBXMNumber != "")
            {
                FXMNO = FBXMNumber;
            }


            string FSTARTYEAR = Convert.ToString(dyFilter["FSTARTYEAR"]);

            string FENDYEAR = Convert.ToString(dyFilter["FENDYEAR"]);

            string FSTARTPERIOD = Convert.ToString(dyFilter["FSTARTPERIOD"]);

            string FENDPERIOD = Convert.ToString(dyFilter["FENDPERIOD"]);

            FPERIOD = FSTARTYEAR + '.' + FSTARTPERIOD + '-' + FENDYEAR + '.' + FENDPERIOD;

            DynamicObject FACCTBOOKID = dyFilter["FACCTBOOKID"] as DynamicObject;
            if (FACCTBOOKID != null)
            {
                FACCTBOOKNAME = Convert.ToString(FACCTBOOKID["Name"]);
                FACCOUNTBOOKID = Convert.ToString(FACCTBOOKID["ID"]);
                FACCTORGID = Convert.ToString(FACCTBOOKID["AccountOrgID_Id"]);
            }

            DynamicObject FCURRENCY = dyFilter["F_PAEZ_CURRENCY"] as DynamicObject;
            if (FCURRENCY != null)
            {
                FCURRENCYID = Convert.ToString(FCURRENCY["ID"]);
            }

            DynamicFormShowParameter param = new DynamicFormShowParameter
            {
                //k556fe65736644b2bb9b38eaebbe44ee6   kfad9647fde24497b9d4108465caabb2as
                FormId = "k556fe65736644b2bb9b38eaebbe44ee6",
                ParentPageId = this.View.PageId
            };

            FPOSTED = Convert.ToString(dyFilter["FNOTPOSTVOUCHER"]);
            if (FPOSTED == "True")
            {
                FPOSTED = "1";
            }
            else if (FPOSTED == "False")
            {
                FPOSTED = "0";
            }
            //显示禁用科目
            FFORBIDSTATUS = Convert.ToString(dyFilter["FFORBIDBALANCE"]);
            if (FFORBIDSTATUS == "True")
            {
                FFORBIDSTATUS = "1";
            }
            else if (FFORBIDSTATUS == "False")
            {
                FFORBIDSTATUS = "0";
            }

            FDepC = Convert.ToString(dyFilter["F_PAEZ_DepC"]);
            if (FDepC == "True")
            {
                FDepC = "1";
            }
            else if (FDepC == "False")
            {
                FDepC = "0";
            }

            param.CustomComplexParams.Add("FSTARTYEAR", FSTARTYEAR);
            param.CustomComplexParams.Add("FENDYEAR", FENDYEAR);
            param.CustomComplexParams.Add("FSTARTPERIOD", FSTARTPERIOD);
            param.CustomComplexParams.Add("FENDPERIOD", FENDPERIOD);
            param.CustomComplexParams.Add("FCURRENCYID", FCURRENCYID);
            param.CustomComplexParams.Add("FPOSTED", FPOSTED);
            param.CustomComplexParams.Add("FFORBIDSTATUS", FFORBIDSTATUS);
            param.CustomComplexParams.Add("FACCOUNTBOOKID", FACCOUNTBOOKID);
            param.CustomComplexParams.Add("FACCTORGID", FACCTORGID);
            param.CustomComplexParams.Add("FAcctNumber", FAcctNumber);
            param.CustomComplexParams.Add("FDepC", FDepC);
            param.CustomComplexParams.Add("FDepNO", FDepNO);
            param.CustomComplexParams.Add("FXMNO", FXMNO);
            param.CustomComplexParams.Add("FPERIOD", FPERIOD);
            param.CustomComplexParams.Add("FACCTBOOKNAME", FACCTBOOKNAME);
            param.CustomComplexParams.Add("FAcctName", FAcctName);
            //this.View.ShowForm(param); FAcctName
           
            this.View.ShowForm(param);

        }

        private void ShowForm(string formId, string pkValue)
        {
            // using Kingdee.BOS.Core.Bill;
            string pageId = Guid.NewGuid().ToString();
            BillShowParameter showParameter = new BillShowParameter();
            showParameter.FormId = "k556fe65736644b2bb9b38eaebbe44ee6";
            showParameter.OpenStyle.ShowType = ShowType.Floating;
            showParameter.PageId = pageId;
            showParameter.Status = OperationStatus.EDIT;
            showParameter.PKey = pkValue;
            this.View.ShowForm(showParameter);
        }
    }
}
